package com.plm.mr.partitioner;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private Text k = new Text();
    private FlowBean v = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 1	13736230513	192.196.100.1	www.atguigu.com	2481	24681	200
        // 2	13846544121	192.196.100.2			264	0	200
        // 1.获取一行数据，转换成字符串
        String line = value.toString();

        // 2.切割数据
        String[] split = line.split("\t");

        // 3.抓取需要的数据：手机号，上行流量，下行流量
        String phone = split[1];
        String up = split[split.length - 3];
        String down = split[split.length -2];

        // 4.封装key和value
        k.set(phone);
        v.setUpFlow(Long.parseLong(up));
        v.setDownFlow(Long.parseLong(down));
        v.setSumFlow();

        // 5.写出
        context.write(k, v);

    }

}
